package newx.com.control.radio
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	
	/**
	 * 单选框 
	 * @author lw
	 * 
	 */
	public class NxRadio extends NxUIComponent
	{
		/**
		 * 总的容器 
		 */
		private var _bgPlace:Sprite;
		
		/**
		 * 选中状态的容器 
		 */
		private var _selectedPlace:Sprite;
		
		/**
		 * 非选中状态的容器 
		 */
		private var _unSelectedPlace:Sprite;
		
		/**热区*/
		private var _hotAreaPlace:Sprite;
		
		/**
		 * 选中状态mc 
		 */
		private var _selectedMC:MovieClip;
		
		/**
		 * 非选中状态mc 
		 */
		private var _unSelectedMC:MovieClip;
		
		/**
		 * 半径 
		 */
		private var _radius:uint;
		
		/**
		 * 皮肤 
		 */
		private var _skin:String;
		
		/**
		 * 是否选中 
		 */
		private var _selected:Boolean;
		
		private var _drawUnSelectedFun:Function;
		
		private var _drawSelectedFun:Function;
		
		public function NxRadio($skin:String = "",$radius:uint = 8)
		{
			_skin	=	$skin;
			
			this.buttonMode = true;
			
			_radius = $radius;
			
			isUseSkin($skin);
			
			init();
		}
		
		private function isUseSkin($skin:String):void
		{
			_skin = $skin;
			
			if(_skin == ""){
				_drawUnSelectedFun = drawUnSelectedBG;
				_drawSelectedFun =drawSelectedBG;
			}else{
				var res:MovieClip =	ResManager.newResClass(_skin) as MovieClip;
				_selectedMC	= res.getChildByName("radioButton_selected") as MovieClip;
				_unSelectedMC =	res.getChildByName("radioButton_unselected") as MovieClip;
				_unSelectedMC.x =_selectedMC.x= 0;
				_unSelectedMC.y =_selectedMC.y= 0;
				
				_drawUnSelectedFun = showUnSelected;
				_drawSelectedFun = showSelected;
			}
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			drawSelect();
		}
		
		public function set skin(val:String):void
		{
			_skin	=	val;
			
			clear(this);
			
			this.buttonMode = true;
			
			isUseSkin(_skin);
			
			init();
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set radius(val:uint):void
		{
			_radius	=	val;
			
			clear(this);
			
			this.buttonMode = true;
			
			isUseSkin(_skin);
			
			init();
		}
		
		public function get radius():uint
		{
			return _radius;
		}
		
		public function get enable():Boolean
		{
			return this.hasEventListener(MouseEvent.CLICK);
		}
		
		public function set enable(boolean:Boolean):void
		{
			if(boolean)
			{
				this.addEventListener(MouseEvent.CLICK,onClick);
			}
			else
			{
				this.removeEventListener(MouseEvent.CLICK,onClick);
			}
		}
		

		private function init():void
		{
			_bgPlace = new Sprite();
			_selectedPlace = new Sprite();
			_unSelectedPlace = new Sprite();
			_hotAreaPlace = new Sprite();
			
			this.addChild(_bgPlace);
			_bgPlace.addChild(_selectedPlace);
			_bgPlace.addChild(_unSelectedPlace);
			
			_bgPlace.addChild(_hotAreaPlace);
			
			drawSelect();
			drawHotArea()
		}
		
		/**
		 * 热区 
		 */
		private function drawHotArea():void
		{
			_hotAreaPlace.graphics.clear();
			_hotAreaPlace.graphics.beginFill(0xff0000,0);
			_hotAreaPlace.graphics.drawCircle(0,0,_radius);
			_hotAreaPlace.graphics.endFill();
		}
		
		private function showSelected():void
		{
			_selectedPlace.addChild(_selectedMC);
		}
		
		private function showUnSelected():void
		{
			_unSelectedPlace.addChild(_unSelectedMC);
		}
		
		/**
		 * 外框
		 */
		private function drawUnSelectedBG():void
		{
			_unSelectedPlace.graphics.beginFill(0,0);
			_unSelectedPlace.graphics.lineStyle(2, 0xFF0000);
			_unSelectedPlace.graphics.drawCircle(0,0,_radius);
			_unSelectedPlace.graphics.endFill();
		}
		
		/**
		 * 选中的原点
		 */
		private function drawSelectedBG():void
		{
			_selectedPlace.graphics.beginFill(0,0);
			_selectedPlace.graphics.lineStyle(2, 0xFF0000);
			_selectedPlace.graphics.drawCircle(0,0,_radius);
			_selectedPlace.graphics.endFill();
			
			_selectedPlace.graphics.beginFill(0xFF0000);
			_selectedPlace.graphics.drawCircle(0,0,_radius/4);
			_selectedPlace.graphics.endFill();
		}
		
		private function clear($place:Sprite):void
		{
			while($place.numChildren > 0 ){
				$place.removeChildAt(0);
			}
			$place.graphics.clear();
		}
		
		private function onClick(e:MouseEvent):void
		{
			selected = !selected;
			
			dispatchEvent(new NxRadioEvent(NxRadioEvent.RADIO_CLICK,this));
		}
		
		/**
		 * 维护选中和非选中状态显示 
		 */
		private function drawSelect():void
		{
			if(_selected){
				clear(_unSelectedPlace);
				_drawSelectedFun();
//				removeListener();
			}else{
				clear(_selectedPlace);
				_drawUnSelectedFun();
				addListener();
			}
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(MouseEvent.CLICK,onClick);
		}
	}
}